package com.yazi.mxz.common.client;

import java.io.IOException;
import java.util.concurrent.Future;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.myframework.smhj.common.tool.WebContextFactoryUtil;


/**
 * 
 * @author mo_yq5
 * @date 2014-12-3
 */
public class JobFactory {
	private static Logger log = LoggerFactory.getLogger(JobFactory.class);
	private static Config config = new DefaultConfig();
	
	/**
	 * 激活商品促销自动开始/结束任务
	 * @param jsonObject
	 */
	protected static void actGoodsPromoteJob(final JsonObject<?> jsonObject) {
		activate(jsonObject, "/goodsPromote");
	}

	/**
	 * 激活订单促销自动开始/结束任务
	 * @param jsonObject
	 */
	protected static void actStorePromoteJob(final JsonObject<?> jsonObject) {
		activate(jsonObject, "/storePromote");
	}
	
	/**
	 * 激活订单自动确认收货任务
	 */
	protected static void actOrderSuccessJob() {
		activate(null, "/order/success");
	}

	/**
	 * 激活订单自动确认退货任务
	 */
	protected static void actOrderReturnedJob() {
		activate(null, "/order/returned");
	}
	
	/**
	 * 激活企业商品信息同步到店铺商品
	 * @param jsonObject
	 */
	protected static void actStoreGoodsSyncJob(final JsonObject<?> jsonObject) {
		activate(jsonObject, "/storeGoods/sync");
	}

	/**
	 * 激活邮件发送任务
	 */
	protected static void actEmailJob() {
		activate(null, "/email");
	}

	/**
	 * 激活短信发送任务
	 */
	protected static void actSmsJob() {
		activate(null, "/sms");
	}

	private static void activate(final JsonObject<?> jsonObject, final String path) {
		if (null == config.getServerPath()) {
			log.debug("未指定JOB请求地址");
			return;
		}
		new Thread(new Runnable() {
			@Override
			public void run() {
				activateJob(jsonObject, path);
			}
		}).start();

	}

	private static void activateJob(JsonObject<?> jsonObject, String path) {

		CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();

		try {
			httpclient.start();
			final HttpPost req = new HttpPost(config.getServerPath() + path);
			req.addHeader("Content-Type", config.getConentType());

			if (null != jsonObject) {
				ObjectMapper om  = (ObjectMapper)WebContextFactoryUtil.getBean("objectMapper");
				om.setFilterProvider(jsonObject.getFilter());
				String body = om.writeValueAsString(jsonObject.getObject());
				StringEntity reqEntity = new StringEntity(body, config.getEncoding());
				req.setEntity(reqEntity);
			}
			Future<HttpResponse> future = httpclient.execute(req, null);
			// and wait until a response is received
			HttpResponse response1 = future.get();
			System.out.println(req.getRequestLine() + "->" + response1.getStatusLine());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void setConfig(Config config) {
		JobFactory.config = config;
	}

}
